package com.aerfafish.spider.service;

import com.aerfafish.spider.redis.CacheStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author aerfafish
 * @date 2021/4/23 3:24 下午
 */
public class LockServiceImpl implements LockService{

    private final static Logger logger = LoggerFactory.getLogger(LockServiceImpl.class);

    @Autowired
    CacheStorage cacheStorage;

    @Override
    public boolean tryLock(String key, long expireTime) {
        try {
            return cacheStorage.acquireLock(key, "LOCKED", expireTime);
        } catch(Exception e) {
            logger.error("get lock by " + key, e);
        }
        return false;
    }
}
